package com.ddpt.platform.config;

import com.ddpt.platform.filter.OpenFilter;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.List;

@Configuration
//@Deprecated
public class FilterConfiguration {


//    @Bean
//    public AdminFilter adminFilter() {
//        return new AdminFilter(redisTemplate);
//    }

    @Bean
    public OpenFilter openFilter() {
        return new OpenFilter();
    }

//    @Bean
//    public SysManageFilter sysManageFilter() {
//        return new SysManageFilter(redisTemplate);
//    }
//    @Bean
//    public CompletionMobileFilter completionMobileFilter(){
//        return new CompletionMobileFilter();
//    }

    private List<String> openUrlList = new ArrayList<>();

    private List<String> excludeurlList = new ArrayList<>();

    private List<String> publicUrlList = new ArrayList<>();

    private List<String> sysManageExcludeList = new ArrayList<>();

    private List<String> completionMobileOpenUrlList = new ArrayList<>();

//    @Bean
//    public JWTHandleFilter jwtHandleFilter() {
//        return new JWTHandleFilter(redisTemplate);
//    }
//
//    /**
//     * jwt用户身份过滤器配置
//     *
//     * @return
//     */
//    @Bean
//    public FilterRegistrationBean filterRegistrationBean() {
//        FilterRegistrationBean<JWTHandleFilter> registrationBean = new FilterRegistrationBean<>();
//        registrationBean.setFilter(jwtHandleFilter());
//        registrationBean.addUrlPatterns("/*");
//        registrationBean.setName("jwtHandleFilter");
//        registrationBean.setOrder(1);
//        registrationBean.addInitParameter("ignorePaths", String.join(",", openUrlList));
//        return registrationBean;
//    }

    @Bean
    public FilterRegistrationBean filterRegistrationBean() {
        FilterRegistrationBean<OpenFilter> registrationBean = new FilterRegistrationBean<>();
        registrationBean.setFilter(openFilter());
        registrationBean.addUrlPatterns("/*");
        registrationBean.setName("openFilter");
        registrationBean.setOrder(1);
        registrationBean.addInitParameter("ignorePaths", String.join(",", openUrlList));
        return registrationBean;
    }

//    @Bean
//    public FilterRegistrationBean filterRegistrationBean2() {
//        FilterRegistrationBean<AdminFilter> registrationBean = new FilterRegistrationBean<>();
//        registrationBean.setFilter(adminFilter());
//        registrationBean.addUrlPatterns("/*");
//        registrationBean.setName("adminFilter");
//        registrationBean.setOrder(2);
//        registrationBean.addInitParameter("excludeUrls", String.join(",", excludeurlList));
//        registrationBean.addInitParameter("publicUrls", String.join(",", publicUrlList));
//        return registrationBean;
//    }

//    @Bean
//    public FilterRegistrationBean filterRegistrationBean3() {
//        FilterRegistrationBean<SysManageFilter> registrationBean = new FilterRegistrationBean<>();
//        registrationBean.setFilter(sysManageFilter());
//        registrationBean.addUrlPatterns("/api/sysManage/*");
//        registrationBean.setName("sysManageFilter");
//        registrationBean.setOrder(3);
//        registrationBean.addInitParameter("excludeUrls", String.join(",", sysManageExcludeList));
//        return registrationBean;
//    }

//    @Bean
//    public FilterRegistrationBean filterRegistrationBean3(){
//        FilterRegistrationBean<CompletionMobileFilter> registrationBean = new FilterRegistrationBean<>();
//        registrationBean.setFilter(completionMobileFilter());
//        registrationBean.addUrlPatterns("/*");
//        registrationBean.setName("completionMobileFilter");
//        registrationBean.setOrder(3);
//        registrationBean.addInitParameter("completionMobileOpenUrl",String.join(",", completionMobileOpenUrlList));
//        return registrationBean;
//    }

    @PostConstruct
    public void init() {
        openUrlList.add("/*");

        //开放

        //健康检查
        excludeurlList.add("/");
        excludeurlList.add("/api/example/.*");
        excludeurlList.add("/api/common/test");
        excludeurlList.add("/api/WW_verify_.*");
        excludeurlList.add("/api/callback/.*");
        excludeurlList.add("/api/auth/.*");
        excludeurlList.add("/api/moment/list");//查询员工朋友圈数据
        excludeurlList.add("/api/moment/config");//查询员工朋友圈配置
        excludeurlList.add("/api/moment/media");
        excludeurlList.add("/api/moment/media/.*");
        excludeurlList.add("/api/test/.*");
        excludeurlList.add("/api/open/finance/.*");
        excludeurlList.add("/api/materialCenter/uploadFileResponse2");    // 素材中心客户端
        excludeurlList.add("/api/materialCenter/client/.*");    // 素材中心客户端
        excludeurlList.add("/api/materialGroup/client/.*");    // 素材分组客户端
        excludeurlList.add("/api/client/index/.*");    // 手机客户端首页
        //第三方活动埋点统计回调接口
        excludeurlList.add("/api/thirdActivityCallBack/.*");
        //第三方活动客户端接口
        excludeurlList.add("/api/thirdActivity/client/.*");
        //官网接口
        excludeurlList.add("/api/officialWebsite/.*");

        // 微信授权相关接口开放
        excludeurlList.add("/api/oauth/wechat/.*");

        // swagger2
        excludeurlList.add("/swagger-ui.html");
        excludeurlList.add("/swagger-ui.html/.*");
        excludeurlList.add("/webjars/.*");
        excludeurlList.add("/webjars");
        excludeurlList.add("/v2/.*");
        excludeurlList.add("/v2");
        excludeurlList.add("/doc.html");
        excludeurlList.add("/swagger-resources/.*");
        excludeurlList.add("/swagger-resources");
        //数据字典接口
        excludeurlList.add("/dictionaries/.*");
        //微信支付回调
        excludeurlList.add("/api/payCallback/.*");

        //登录后开放
        publicUrlList.add("/api/file/.*");
        publicUrlList.add("/api/common/.*");
        publicUrlList.add("/api/homePage/.*");
        //活动相关，测试使用开放权限
        //publicUrlList.add("/api/act/.*");
        //publicUrlList.add("/api/activity/.*");

        publicUrlList.add("/api/validateDomain/upload");

        // 管理端后台登录开发
        sysManageExcludeList.add("/api/sysManage/user/login");

        // 微信授权相关接口开放
        excludeurlList.add("/api/oauth/wechat/.*");
        excludeurlList.add("/api/oauth/.*");
    }

    @PostConstruct
    public void killBug() {
        System.out.println("\n" +
                "                    _ooOoo_\n" +
                "                   o8888888o\n" +
                "                   88\" . \"88\n" +
                "                   (| -_- |)\n" +
                "                    O\\ = /O\n" +
                "                ____/`---'\\____\n" +
                "                . ' \\\\| |// `.\n" +
                "               / \\\\||| : |||// \\\n" +
                "             / _||||| -:- |||||- \\\n" +
                "               | | \\\\\\ - /// | |\n" +
                "             | \\_| ''\\---/'' | |\n" +
                "              \\ .-\\__ `-` ___/-. /\n" +
                "           ___`. .' /--.--\\ `. . __\n" +
                "        .\"\" '< `.___\\_<|>_/___.' >'\"\".\n" +
                "       | | : `- \\`.;`\\ _ /`;.`/ - ` : | |\n" +
                "         \\ \\ `-. \\_ __\\ /__ _/ .-` / /\n" +
                " ======`-.____`-.___\\_____/___.-`____.-'======\n" +
                "                    `=---='\n" +
                "\n" +
                "               佛祖保佑 永无BUG\n");
    }

}
